// -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SuppressEventReceivers.cs
//  Developer:	Igor Solomatov
//  Created:	03.03.2013
// 
// -------------------- iSharePoint -------------------- //

using System;
using Microsoft.SharePoint;

namespace Smart.Common
{
	public class SuppressEventReceivers : SPEventReceiverBase, IDisposable
	{
		private bool _eventFiringEnabled;

		public SuppressEventReceivers()
		{
			_eventFiringEnabled = EventFiringEnabled;

			EventFiringEnabled = false;
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		/// <filterpriority>2</filterpriority>
		public void Dispose()
		{
			try
			{
				if (_eventFiringEnabled)
				{
					EventFiringEnabled = _eventFiringEnabled;
					_eventFiringEnabled = false;
				}
			}
			catch (Exception x)
			{
				H.LX("SuppressEventReceivers.Dispose", x);
			}

			GC.SuppressFinalize(this);
		}

		~SuppressEventReceivers()
		{
			if (_eventFiringEnabled)
				H.LT("SuppressEventReceivers.Finalize.InvalidOperationException: SuppressEventReceivers is not properly disposed!");
		}
	}
}